package cn.soulic.tools.redis.migration.handler;

import cn.soulic.tools.redis.migration.config.RedisMigrationProperties;
import io.lettuce.core.ScanArgs;
import io.lettuce.core.ScanCursor;
import io.lettuce.core.ScoredValue;
import io.lettuce.core.ScoredValueScanCursor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Sorted Set数据处理
 *
 * @author Soulic Yang
 * @since 1.0.0
 */
@Component(value = "zset")
public class SortedSetHandler extends AbstractHandler {
    private final Logger logger = LoggerFactory.getLogger(SortedSetHandler.class);

    @Resource
    private RedisMigrationProperties properties;

    @Override
    public void doHandle(String key) {
        ScanCursor zScanCursor = ScanCursor.INITIAL;
        ScanArgs matchesAll = ScanArgs.Builder.matches(MATCHER_ALL).limit(properties.getMaxCount());

        do {
            ScoredValueScanCursor<String> cursor = sourceCommands.zscan(key, zScanCursor, matchesAll);
            List<ScoredValue<String>> values = cursor.getValues();
            targetCommands.zadd(key, values.toArray(new ScoredValue[0]));
            zScanCursor = cursor;
        } while (!zScanCursor.isFinished());
    }
}
